package com.main;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import java.io.File;

import com.barcode.ScanActivity;
//import com.barcode.BarcodeActivity_Jar;
import com.electrictorch.ElectrictorchActivity;
import com.electrictorch.FlashActivity;
import com.example.ht380d4G_jar_demo.R;
import com.bluetoothprint.BlueToothActivity;
import com.main.ShowPicDialog;
import com.main.MainActivity;
import com.nfc.NFCActivity;
import com.meter.MeterActivity;
import com.powersercurity.PowerSecurityActivity;
import com.psam.PSamActivity;
import com.rs232.RS232Activity;
import com.simpleIc.SimpleIcActivity;
import com.tesam.TesamActivity;
import com.zebra.sdl.SDLguiActivity;

public class MainActivity extends Activity implements OnClickListener {

	private Button navi_button_oned_soft_scan,navi_button_oned_hard_scan, navi_button_psam, navi_button_rs232,
			navi_button_caobiao, navi_button_t485 , navi_button_simpleic , navi_button_tesam , navi_button_powersercurity ,
			navi_button_electric,navi_button_flash,nfcBtn2,bluetoothBtn , cameraBtn;

	private boolean mDTMFToneEnabled;// 按键操作音
	private Object mToneGeneratorLock = new Object();// 监视器对象锁
	private static final int TONE_RELATIVE_VOLUME = 80;
	private static final int TONE_LENGTH_MS = 150;// 延迟时间
	private ToneGenerator mToneGenerator;
    private boolean flag = true;
	private static final int REQUEST_CAMERA_CAPTURE = 100;

	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			
		};
	};
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
//		if(BaseApplication.getProperty("ro.product.name","JB-HT368D").equals("HT380K")){
//		Intent oneIntent = new Intent(this, SDLguiActivity.class);
//		startActivityForResult(oneIntent, 0);
//		}
	}

//	@Override
//	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		// TODO Auto-generated method stub
//		super.onActivityResult(requestCode, resultCode, data);
//		if(BaseApplication.getProperty("ro.product.name","JB-HT368D").equals("HT380K")){
//		if (requestCode == 0) {
//			System.out.println("onActivityResult");
//			finish();
//		}
//		}
//	}
	private void initView() {
		navi_button_oned_soft_scan = (Button) findViewById(R.id.navi_button_oned_soft_scan);
		navi_button_oned_hard_scan = (Button) findViewById(R.id.navi_button_oned_hard_scan);
		cameraBtn = (Button) findViewById(R.id.navi_button_camera);
//		navi_button_psam = (Button) findViewById(R.id.navi_button_psam);
//		navi_button_rs232 = (Button) findViewById(R.id.navi_button_rs232);
//		navi_button_caobiao = (Button) findViewById(R.id.navi_button_caobiao);
//		navi_button_t485 = (Button) findViewById(R.id.navi_button_t485);
//		navi_button_simpleic = (Button) findViewById(R.id.navi_button_simpleic);
//		navi_button_tesam = (Button) findViewById(R.id.navi_button_tesam);
//		navi_button_powersercurity = (Button) findViewById(R.id.navi_button_powersercurity);
		navi_button_electric = (Button) findViewById(R.id.navi_button_electric);
		//navi_button_flash = (Button)findViewById(R.id.navi_button_flash);
		bluetoothBtn = (Button) findViewById(R.id.navi_bluetooth_printer);
		nfcBtn2 = (Button) findViewById(R.id.navi_button_rfid);
		if(!exist("/dev/moto_sdl")){
			navi_button_oned_soft_scan.setVisibility(View.INVISIBLE);
		}else {
			navi_button_oned_hard_scan.setVisibility(View.GONE);
		}
		navi_button_oned_soft_scan.setOnClickListener(this);
		navi_button_oned_hard_scan.setOnClickListener(this);
//		navi_button_psam.setOnClickListener(this);
//		navi_button_rs232.setOnClickListener(this);
//		navi_button_caobiao.setOnClickListener(this);
//		navi_button_t485.setOnClickListener(this);
//		navi_button_simpleic.setOnClickListener(this);
//		navi_button_tesam.setOnClickListener(this);
//		navi_button_powersercurity.setOnClickListener(this);
		cameraBtn.setOnClickListener(this);
		cameraBtn.setVisibility(View.GONE);
		navi_button_electric.setOnClickListener(this);
		//navi_button_flash.setOnClickListener(this);
		//navi_button_flash.setVisibility(View.GONE);
		nfcBtn2.setOnClickListener(this);
		bluetoothBtn.setOnClickListener(this);
		mDTMFToneEnabled = Settings.System.getInt(this.getContentResolver(),
				Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
		synchronized (mToneGeneratorLock) {
			if (mToneGenerator == null) {
				try {
					mToneGenerator = new ToneGenerator(
							AudioManager.STREAM_DTMF, TONE_RELATIVE_VOLUME);
				} catch (RuntimeException e) {
					Log.d("jiebao",
							"Exception caught while creating local tone generator: "
									+ e);
					mToneGenerator = null;
				}

			}
		}
	}

	private boolean exist(String path){
		File file = new File(path);
		if (!file.exists()) {
			return false;
		}else
			return true;
		
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.navi_button_oned_soft_scan:
			Intent softIntent = new Intent(this, SDLguiActivity.class);
			startActivity(softIntent);
			break;
		case R.id.navi_button_oned_hard_scan:
			Intent hardIntent = new Intent(this, ScanActivity.class);
			hardIntent.putExtra("Model", "oneModel");
			startActivity(hardIntent);
			break;
			
//		case R.id.navi_button_psam:
//			startActivity(new Intent(this, PSamActivity.class));
//			break;
//
//		case R.id.navi_button_rs232:
//			startActivity(new Intent(MainActivity.this, RS232Activity.class));
//			break;
//
//		case R.id.navi_button_caobiao:
//			Intent intentd3 = new Intent(this, MeterActivity.class);
//			intentd3.putExtra("From", "caobiao");
//			intentd3.putExtra("IS485", false);
//			startActivity(intentd3);
//			break;
//
//		case R.id.navi_button_simpleic:
//			Intent intentd4 = new Intent(this, SimpleIcActivity.class);
//			startActivity(intentd4);
//			break;
//			
//		case R.id.navi_button_t485:
//			Intent intentd = new Intent(this, MeterActivity.class);
//			intentd.putExtra("From", "caobiao");
//			intentd.putExtra("IS485", true);
//			startActivity(intentd);
//			break;
//			
//		case R.id.navi_button_powersercurity:
//			Intent intentd5 = new Intent(this, PowerSecurityActivity.class);
//			startActivity(intentd5);
//			break;
//			
//		case R.id.navi_button_tesam:
//			startActivity(new Intent(this, TesamActivity.class));
//			break;
			
		case R.id.navi_button_electric:
			startActivity(new Intent(this, ElectrictorchActivity.class));
			break;
//		case R.id.navi_button_flash:
//			startActivity(new Intent(this, FlashActivity.class));
//			break;
		case R.id.navi_bluetooth_printer:

			startActivity(new Intent(this, BlueToothActivity.class));
			break;
		case R.id.navi_button_rfid:
			playTone(ToneGenerator.TONE_DTMF_1);
			startActivity(new Intent(this, NFCActivity.class));
			break;
		case R.id.navi_button_camera:
			playTone(ToneGenerator.TONE_DTMF_1);
			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			startActivityForResult(intent, REQUEST_CAMERA_CAPTURE);
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == REQUEST_CAMERA_CAPTURE) {
			if (resultCode == RESULT_OK) {
				Bitmap bmp = (Bitmap) data.getExtras().get("data");
				ShowPicDialog dialog = new ShowPicDialog(this, bmp);
				dialog.show();
			}
		}

	}
	private void playTone(int tone) {
		if (!mDTMFToneEnabled) {
			return;
		}
		AudioManager audioManager = (AudioManager) MainActivity.this
				.getSystemService(Context.AUDIO_SERVICE);
		int ringerMode = audioManager.getRingerMode();
		if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
				|| (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
			return;
		}

		synchronized (mToneGeneratorLock) {
			if (mToneGenerator == null) {
				Log.d("jiebao", "playTone: mToneGenerator == null, tone: " + tone);
				return;
			}
			mToneGenerator.startTone(tone, TONE_LENGTH_MS);
		}
	}
}
